<!--
  This file is a part of the open-eBackup project.
  This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
  If a copy of the MPL was not distributed with this file, You can obtain one at
  http://mozilla.org/MPL/2.0/.
  
  Copyright (c) [2024] Huawei Technologies Co.,Ltd.
  
  THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
  EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
  MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
  -->

<div *ngIf="rowCopy.status === dataMap.copydata_validStatus.invalid.value">
    <lv-alert lvType="warning" lvClosable="false">
        {{'protection_invalid_recovery_label' | i18n }}
    </lv-alert>
</div>
<ng-container *ngIf="isDiskRestore">
    <h3 class="aui-gutter-column-md">{{'protection_recovery_selected_disk_label' | i18n}}</h3>
    <div class="recovery-table aui-gutter-column-xl">
        <lv-pro-table [data]="originDiskTableData" [config]="originDiskTableConfig"></lv-pro-table>
    </div>
    <h3 class="aui-gutter-column-md">{{'protection_restore_file_config_label' | i18n}}</h3>
</ng-container>
<lv-form [formGroup]="formGroup" [lvLabelColon]="false" class="formGroup" lvKeepRequiredWidth>
    <lv-form-item>
        <lv-form-label>
            {{ 'protection_restore_to_label' | i18n }}
        </lv-form-label>
        <lv-form-control>
            <lv-radio-group formControlName="restoreTo" [lvGroupName]="'restoreToGroup'">
                <lv-group [lvGutter]="'20px'">
                    <lv-radio [lvValue]="restoreLocationType.ORIGIN" [lvDisabled]="restoreToNewLocationOnly"
                        [lv-tooltip]="restoreToNewLocationOnly ? ('protection_cloud_origin_restore_disabled_label' | i18n) : ''">
                        {{ 'common_restore_to_origin_location_label' | i18n }}
                    </lv-radio>
                    <lv-radio [lvValue]="restoreLocationType.NEW">
                        {{ 'common_restore_to_new_location_label' | i18n }}
                    </lv-radio>
                </lv-group>
            </lv-radio-group>
        </lv-form-control>
    </lv-form-item>
    <ng-container *ngIf="formGroup.value.restoreTo === restoreLocationType.ORIGIN">
        <lv-form-item>
            <lv-form-label>
                {{'common_location_label' | i18n}}
            </lv-form-label>
            <lv-form-control>
                <span lv-overflow style="max-width: 840px;">{{originLocation | nil}}</span>
            </lv-form-control>
        </lv-form-item>
    </ng-container>
    <lv-form-item *ngIf="!isDiskRestore">
        <lv-form-label lvRequired>{{'protection_new_vm_name_label' | i18n}}</lv-form-label>
        <lv-form-control [lvErrorTip]="nameErrorTip">
            <input lv-input type="text" formControlName="vmName" />
        </lv-form-control>
    </lv-form-item>
    <ng-container *ngIf="formGroup.value.restoreTo === restoreLocationType.NEW">
        <ng-container *ngIf="rowCopy?.resource_type === dataMap.Resource_Type.cNware.value">
            <lv-form-item>
                <lv-form-label lvRequired>
                    {{'protection_volume_restore_target_label' | i18n}}
                </lv-form-label>
                <lv-form-control>
                    <lv-tree-select [lvTreeData]="serverTreeData" lvTreeSelectionMode="single"
                        lvPlaceholder="{{(isDiskRestore ? 'common_select_vm_label' : 'protection_select_host_placeholder_label') | i18n}}"
                        formControlName="targetServer" lvShowClear lvTreeShowLine lvTreeShowContentIcon
                        (lvExpandedChange)="expandedChange($event)" lvShowFilter lvFilterKey="label" lvFilterMode="contains">
                        <lv-treeselect-trigger>
                            <span>{{ formGroup.value.targetServer[0]?.path }}</span>
                        </lv-treeselect-trigger>
                    </lv-tree-select>
                </lv-form-control>
            </lv-form-item>
        </ng-container>
        <ng-container *ngIf="rowCopy?.resource_type === dataMap.Resource_Type.nutanix.value">
            <lv-form-item>
                <lv-form-label lvRequired>
                    {{'protection_volume_restore_target_label' | i18n}}
                </lv-form-label>
                <lv-form-control>
                    <lv-select [lvOptions]="nutaniHostOptions" lvValueKey='value'
                    formControlName="targetServer" lvShowClear lvMode="multiple"
                    lvShowFilter lvFilterKey='label' lvFilterMode='contains'></lv-select>
                </lv-form-control>
            </lv-form-item>
        </ng-container>
        
        <ng-container *ngIf="restoreType === restoreV2Type.InstanceRestore">
            <lv-form-item>
                <lv-form-label lvRequired>
                    {{'protection_target_storage_pool_label' | i18n}}
                    <i lv-icon="aui-icon-help" lv-tooltip="{{'protection_target_pool_help_label' | i18n}}"
                        lvTooltipPosition="rightTop" lvTooltipTheme="light" class="configform-constraint"
                        lvColorState='true'></i>
                </lv-form-label>
                <lv-form-control>
                    <lv-radio-group formControlName="targetPool">
                        <lv-group [lvGutter]="'20px'" class="mgt-8" lvDirection='vertical'>
                            <lv-radio [lvValue]="storageLocation.Same">
                                {{'protection_same_storage_pool_label' | i18n}}
                            </lv-radio>
                            <lv-radio [lvValue]="storageLocation.Different">
                                {{'protection_different_storage_pool_label' | i18n}}
                            </lv-radio>
                        </lv-group>
                    </lv-radio-group>
                </lv-form-control>
            </lv-form-item>
            <lv-form-item *ngIf="formGroup.value.targetPool === storageLocation.Same">
                <lv-form-label lvRequired>{{'protection_select_storage_pool_label' | i18n}}</lv-form-label>
                <lv-form-control>
                    <lv-select [lvOptions]="targetDatastoreOptions" lvValueKey='value'
                        formControlName="targetRecoveryPool" [lvContentTemplate]="poolContentTpl" lvShowClear
                        lvShowFilter lvFilterKey='label' lvFilterMode='contains'>
                    </lv-select>
                </lv-form-control>
            </lv-form-item>
        </ng-container>
    </ng-container>
    <div class="instance-restore-table"
        *ngIf="formGroup.value.restoreTo === restoreLocationType.NEW && restoreType === restoreV2Type.InstanceRestore">
        <ng-container *ngIf="formGroup.value.targetPool === storageLocation.Same; else elseIrTableTemplate">
            <lv-pro-table [data]="recoveryDiskTableData" [config]="irDiskTableConfig"></lv-pro-table>
        </ng-container>
        <ng-template #elseIrTableTemplate>
            <lv-pro-table [data]="recoveryDiskTableData" [config]="irDiffDiskTableConfig"></lv-pro-table>
        </ng-template>
    </div>
    <ng-container
        *ngIf="(isDiskRestore || (!isDiskRestore && formGroup.value.restoreTo === restoreLocationType.NEW)) && restoreType !== restoreV2Type.InstanceRestore">
        <lv-form-item>
            <lv-form-label lvRequired>
                {{'protection_cnware_vm_disk_file_label' | i18n}}
            </lv-form-label>
            <lv-form-control>
                <lv-pro-table [config]="recoveryDiskTableConfig" [data]="recoveryDiskTableData"></lv-pro-table>
            </lv-form-control>
        </lv-form-item>
    </ng-container>
    <ng-container *ngIf="!isDiskRestore">
        <lv-form-item>
            <lv-form-label lvRequired>{{'common_network_location_label' | i18n}}</lv-form-label>
            <lv-form-control>
                <lv-pro-table [data]="networkTableData" [config]="networkTableConfig"></lv-pro-table>
            </lv-form-control>
        </lv-form-item>
        <lv-form-item>
            <lv-form-label>
                {{'protection_startup_network_adaptor_label' | i18n}}
                <i lv-icon="aui-icon-help"
                    lv-tooltip="{{'protection_cnware_restore_network_help_label' | i18n}}"
                    lvTooltipPosition="rightTop" lvTooltipTheme="light" class="configform-constraint"
                    lvColorState='true'></i>
            </lv-form-label>
            <lv-form-control>
                <lv-switch formControlName="openInterface"></lv-switch>
                <div class="aui-text-help" *ngIf="formGroup.value.openInterface">
                    {{'protection_startup_network_adaptor_warn_label' | i18n}}
                </div>
            </lv-form-control>
        </lv-form-item>
    </ng-container>
    <div class="advanced">
        <lv-collapse [lvMultiExpansion]="'false'" lvType="simple">
            <lv-collapse-panel [lvTitle]="titleTpl" [lvExpanded]="true">
                <lv-form-item>
                    <lv-form-label>
                        {{'protection_proxy_host_label' | i18n}}
                    </lv-form-label>
                    <lv-form-control>
                        <lv-select [lvOptions]="proxyOptions" formControlName="proxyHost" lvValueKey='value'
                            lvShowCheckAll lvShowFilter lvFilterKey="label" lvFilterMode="contains" lvMode="multiple"
                            [lvContentTemplate]="contentTpl"
                            lvPlaceholder="{{'protection_agent_placeholder_label' | i18n}}">
                        </lv-select>
                        <ng-template #contentTpl let-item>
                            <lv-group lvGutter="4px">
                                <i lv-icon="{{
                                      item.linkStatus === dataMap.resource_LinkStatus_Special.normal.value ? 'aui-host-online' : 'aui-host-offline'
                                    }}"></i>
                                <span>{{ item.label }}</span>
                            </lv-group>
                        </ng-template>
                    </lv-form-control>
                </lv-form-item>
                <lv-form-item>
                    <lv-form-label>{{'protection_auto_power_on_label' | i18n}}</lv-form-label>
                    <lv-form-control>
                        <lv-switch formControlName="restoreAutoPowerOn"></lv-switch>
                    </lv-form-control>
                </lv-form-item>
                <!-- 删除原机 -->
                <lv-form-item
                    *ngIf="restoreType === restoreV2Type.InstanceRestore && formGroup.value.restoreTo === restoreLocationType.ORIGIN">
                    <lv-form-label>{{'protection_delete_origin_vm_label' | i18n}}</lv-form-label>
                    <lv-form-control>
                        <lv-switch formControlName="cleanOriginVM"></lv-switch>
                        <span *ngIf="formGroup.value.cleanOriginVM" class="delete-vm-mgl">
                            <i lv-icon="lv-icon-status-warning-info" lvColorState='true' class="type-icon"></i>
                            <span>
                                {{'protection_delete_origin_vm_warn_label' | i18n}}
                            </span>
                        </span>
                    </lv-form-control>
                </lv-form-item>
                <lv-form-item>
                    <lv-form-label>{{'common_restore_before_copy_verify_label' | i18n}}
                        <i lv-icon="aui-icon-help"
                            lv-tooltip="{{'protect_hcs_restore_before_copy_verify_label' | i18n}}"
                            lvTooltipPosition="rightTop" lvTooltipTheme="light" class="configform-constraint"
                            lvColorState='true'></i>
                    </lv-form-label>
                    <lv-form-control>
                        <lv-switch formControlName="copyVerify"
                            [lv-tooltip]="verifyStatus === CopyDataVerifyStatus.noGenerate.value ? copyVerifyDisableLabel : ''"
                            [lvDisabled]="verifyStatus === CopyDataVerifyStatus.noGenerate.value">
                        </lv-switch>
                    </lv-form-control>
                </lv-form-item>
            </lv-collapse-panel>
        </lv-collapse>
    </div>
</lv-form>

<ng-template #titleTpl>
    <lv-group lvGutter='8px'>
        <span class="custom-collapse-title-text advanced-title">{{'common_advanced_label'|i18n}}</span>
    </lv-group>
</ng-template>

<ng-template #datastoreTpl let-item>
    <lv-select class="type-select" [lvOptions]="item.datastoreOptions" [(ngModel)]="item.recoveryDatastore"
        (ngModelChange)='datastoreChange(item)' lvValueKey='value' lvShowClear lvShowFilter lvFilterKey='label'
        lvFilterMode='contains' [lvContentTemplate]="poolContentTpl">
    </lv-select>
</ng-template>

<ng-template #recoveryTypeTpl let-item>
    <lv-select class="type-select" [lvOptions]="typeOptions" [(ngModel)]="item.recoveryDiskType"
        (ngModelChange)='diskTypeChange($event, item)' lvValueKey='value'>
    </lv-select>
</ng-template>

<ng-template #targetDiskTpl let-item>
    <ng-container *ngIf="item.recoveryDiskType === NEW_DISK; else elseTemplate">
        <lv-group [lvColumns]='["auto", "16px", "260px"]' lvRowGutter="4px">
            <input lv-input type="text" [(ngModel)]="item.targetDiskName"
                placeholder="{{'protection_recovery_new_disk_placeholder_label' | i18n}}"
                (ngModelChange)="diskNameChange($event, item)" [ngClass]="{'error-status': item.nameInvalid}"/>
            <div class="error-tip">
                <i *ngIf="item.nameInvalid" [lv-tooltip]="item.nameErrorTip" [lvTooltipVisible]="true"
                    lvTooltipPosition="right" lv-icon="lv-icon-status-failed"></i>
            </div>
            <lv-select [lvOptions]="item.datastoreOptions" [(ngModel)]="item.recoveryDatastore" lvValueKey='value'
                (ngModelChange)="datastoreChange()" lvPlaceholder="{{'protection_select_datastore_label' | i18n}}"
                [lvContentTemplate]="poolContentTpl" lvShowClear lvShowFilter lvFilterKey='label'
                lvFilterMode='contains'>
            </lv-select>
        </lv-group>
    </ng-container>
    <ng-template #elseTemplate>
        <lv-select class="type-select" [lvOptions]="item.diskOptions" [(ngModel)]="item.recoveryDisk"
            (ngModelChange)='diskChange($event, item)' lvValueKey='uuid' lvShowClear lvShowFilter lvFilterKey='label'
            lvFilterMode='contains'>
        </lv-select>
    </ng-template>
</ng-template>

<ng-template #sizeTpl let-item>
    <span lv-overflow>
        {{item.volSizeInBytes | capacityCalculateLabel:'1.0-0':unitconst.BYTE:false}}
    </span>
</ng-template>

<ng-template #networkNameTpl let-item>
    <span lv-overflow>{{item.parentName ? item.name + ' (' + item.parentName + ')' : item.name}}</span>
</ng-template>

<ng-template #portGroupTpl let-item>
    <lv-select class="type-select" [lvOptions]="item.portGroupOptions" [(ngModel)]="item.recoveryPortGroup"
        (ngModelChange)='portGroupChange()' lvValueKey='uuid' lvShowClear lvShowFilter lvFilterKey='label'
        lvFilterMode='contains'>
    </lv-select>
</ng-template>

<ng-template #targetIpTpl let-item>
    <input lv-input type="text" [(ngModel)]="item.targetIp" />
</ng-template>

<ng-template #poolContentTpl let-item>
    <lv-group class="datastore-group" lvGutter='14px'>
        <span lv-overflow>{{ item.label }}</span>
        <span class="aui-text-desc">
            {{'protection_remain_capacity_label' | i18n:[item.showCapacity |
            capacityCalculateLabel:'1.1-3':unitconst.BYTE:false]}}
        </span>
    </lv-group>
</ng-template>

<ng-template #preallocationTpl let-item>
    <lv-select class="type-select" [lvOptions]="getPreallocationOptions(item)" [(ngModel)]="item.recoveryPreallocation"
        (ngModelChange)='preallocationChange()' lvValueKey='value' lvShowClear>
    </lv-select>
</ng-template>

<ng-template #thExtHelp>
    <div class="custom-icon cnware-help">
        <i lv-icon="aui-icon-help"
            lv-tooltip="{{(isDiskRestore ? 'protection_cnware_new_disk_name_help_label' : 'protection_target_pool_help_label') | i18n}}"
            lvTooltipPosition="rightTop" lvTooltipTheme="light" lvColorState='true'></i>
    </div>
</ng-template>

<ng-template #originalPreallocation let-item>
    <span lv-overflow>{{item.provisionType | textMap:'preallocationType'}}</span>
</ng-template>